#include "stdafx.h"
#include "common.h"
#include "allData.h"
#include "serializer\serializer.h"
#include <iostream>

using namespace std;

identifiable::identifiable()
{
	idx=0;
	if (globalIdManager) 
        idx=globalIdManager->getNewId();
	identifiables[idx]=this;
	bearing=0;
	setId(typeid(*this).name());
}
identifiable::identifiable(vector2& _crds, float b) : coords(_crds), bearing(b)
{
	idx=0;
	if (globalIdManager) idx=globalIdManager->getNewId();
	identifiables[idx]=this;
	setId(typeid(*this).name());
}
identifiable::~identifiable()
{
//ACHTUNG!
	identifiables.erase(this->idx);
}

void identifiable::serialize(Serializer& serializer)
{
	serializer << idx << coords.x << coords.y << bearing;
}

int getSupplyMethodFromRouteType(int rtype)
{
	switch (rtype)
	{
	case RT_BATTLE:
		return 0;
	case RT_SUPPLY_SHIP:
		return SM_SHIP;
	case RT_SUPPLY_TRAIN:
		return SM_TRAIN;
	case RT_SUPPLY_TRUCK:
			return SM_TRUCK;
	default:
		return SM_TRUCK;

	}
}

void log_that(int level, char* what,char* what2, char* what3,char* what4, char* what5,char* what6,char* what7,char* what8,char* what9,char* what10,char* what11)
{
	if (g_log_level && LOGF)
	{
		if (level <= g_log_level)
		{

			std::string cost("");
			time_t now;
			time(&now);
			struct tm when;
			when = *localtime(&now);

			char buf[255];
			std::string val,val2;

			val = itoa(when.tm_mday,(char*)buf,10);
			fputs(val.c_str(),LOGF);
			cost.append(val.c_str());
			fputs("-",LOGF);
			cost.append("-");

			val = itoa(when.tm_mon,(char*)buf,10);
			fputs(val.c_str(),LOGF);
			cost.append(val.c_str());
			fputs("-",LOGF);
			cost.append("-");
			
			val = itoa(when.tm_year,(char*)buf,10);
			fputs(val.c_str(),LOGF);
			cost.append(val.c_str());
			fputs(" ",LOGF);
			cost.append("-");
			
			val = itoa(when.tm_hour,(char*)buf,10);
			if (val.size() <2) 
			{
				val2=val;
				val = "0";
				val.append(val2.c_str());
			}			
			fputs(val.c_str(),LOGF);
			cost.append(val.c_str());
			fputs(":",LOGF);
			cost.append(":");
			
			val = itoa(when.tm_min,(char*)buf,10);
			if (val.size() <2) 
			{
				val2=val;
				val = "0";
				val.append(val2.c_str());
			}			
			fputs(val.c_str(),LOGF);
			cost.append(val.c_str());
			fputs(":",LOGF);
			cost.append(":");
			
			val = itoa(when.tm_sec,(char*)buf,10);
			if (val.size() <2) 
			{
				val2=val;
				val = "0";
				val.append(val2.c_str());
			}			
			fputs(val.c_str(),LOGF);
			cost.append(val.c_str());

			for (int i=1; i<= level*4; i++)
			{
				fputs(" ",LOGF);
				cost.append(" ");
			}

			fputs(what,LOGF);
			
			if (what2)
				fputs(what2,LOGF);
			if (what3)
				fputs(what3,LOGF);
			if (what4)
				fputs(what4,LOGF);
			if (what5)
				fputs(what5,LOGF);
			if (what6)
				fputs(what6,LOGF);
			if (what7)
				fputs(what7,LOGF);
			if (what8)
				fputs(what8,LOGF);
			if (what9)
				fputs(what9,LOGF);
			if (what10)
				fputs(what10,LOGF);
			if (what11)
				fputs(what11,LOGF);

			cost.append(what);
			
			if (what2)
				cost.append(what2);
            if (what3)
                cost.append(what3);
			if (what4)
				cost.append(what4);
			if (what5)
				cost.append(what5);
			if (what6)
				cost.append(what6);
			if (what7)
				cost.append(what7);
			if (what8)
				cost.append(what8);
			if (what9)
				cost.append(what9);
			if (what10)
				cost.append(what10);
			if (what11)
				cost.append(what11);

			cout << cost.c_str();
	
		}
	}
}
